<?php
namespace My\Controller;
use Utuo\BackgroundController;
class MicroThemeController extends BackgroundController {
	
	private $model = null;
	
	// 应用到模型
	private $applyTo = null;
	private $id=0;
	
	// 映射模型与控件器
	private $map = array(
		'PartnerProduct' => 'Shop',
	);
	
	public function _initialize() {
		$this->model = I("model", "");
		$this->applyTo = I("apply_to", "") or $this->applyTo = $this->model;
		$this->id=I('id');
		
		$this->setControllerTitle('主题设置');
		$this->setActionTitle(array(
				'index' => '主题选择',
				'config' => '样式设置',
		));
	}
	
	// 主题选择
	public function indexAction() {
		$model = M($this->applyTo)->find($this->id);
	    
		$modelId = M("Model")->where("tag='{$this->model}'")->getField("id");
		$customerTheme = M('theme')->where("customer=".$this->aid." and  status=1 and is_deleted=0")->select();
		
		if($customerTheme){
		$themeLists = M()->table("(select this0.* from (
				select *
				from __THEME__ this0
				where (this0.partner in ( SELECT partner FROM __CUSTOMER__ where id = {$this->aid} )) and this0.model_id=$modelId and this0.status = 1 and this0.is_deleted=0
				) this0
				where this0.customer in(0, {$this->aid})) this1")->order("this1.partner,this1.customer")->getField("this1.id, this1.image, this1.config, this1.name", true);
		}else {
			$themeLists = M()->table("(select this0.* from (
					select *
					from __THEME__ this0
					where ((this0.partner = 0) or (this0.partner in ( SELECT partner FROM __CUSTOMER__ where id = {$this->aid} ))) and this0.model_id=$modelId and this0.status = 1 and this0.is_deleted=0
					) this0
					where this0.customer in(0, {$this->aid})) this1")->order("this1.partner,this1.customer")->getField("this1.id, this1.image, this1.config, this1.name", true);
		}
		foreach ($themeLists as &$v) $v['config'] = unserialize($v['config']);
		
		$this->assign("themeLists", $themeLists);
		$this->assign("themeId", $model['theme']);
		$this->assign("did",$this->id);
		$this->display();
	}
	
	// 本色设置
	public function configAction() {
		$themeId = I('id', 0, "intval");
		$theme = M("Theme")->find($themeId);
		$condition = array('cid' => $this->aid,
						   'theme' => $theme['id']);
		$m = M("ThemeConfig");
		$themeConfig = $m ->where($condition)->find();
		if(!$themeConfig) {
			if($id = $m->add($condition) and $id===false) {
				$this->error("添加颜色配置失败");
				exit;
			}
			M($this->applyTo)->where('id=%d and theme=%d', $this->aid, $themeId)->setField('theme_color', $id);
			$themeConfig = $m->find($id);
		}
		$all_config = unserialize($theme["config"]);
		$config = unserialize($themeConfig['config']);
		foreach ($all_config as &$v) $v["value"] = $config[$v['varName']]; // 绑定参数值
		$this->assign("config", $all_config);
		$this->assign("indexUrl", url('Home', isset($this->map[$this->model])?$this->map[$this->model]:$this->model, 'index', "sid={$this->aid}&tid=$themeId"));
		$this->assign('theme', $theme);
		$this->display();
	}
	
	// 设置主题
	public function setThemeAction() {
		$theme = I("theme", 0, "intval");
	    $id=I('did');
		false !== M($this->applyTo)->where("id=%d",  $id)->setField("theme", $theme) and $this->ajaxSuccess("设为默认主题成功");
		
		$this->ajaxError("设置默认主题失败");
	}
	
	// 提交设置
	public function savePostAction() {
		$themeId = I('id', 0, "intval");
		$d = D("ThemeConfig");
		$theme = M("Theme")->find($themeId);
		$themeConfig = array();
		$data = I("config");
		if(is_array($data)) {
			foreach ((array)unserialize($theme["config"]) as $v) {
				empty($data[$v["varName"]]) or $themeConfig[$v["varName"]] = $data[$v["varName"]];
			}
		}
		false !== $d->where(array('cid'=>$this->aid, 'theme' => $theme['id']))->setField("config", serialize($themeConfig)) and $this->ajaxSuccess("颜色配置成功");
		$this->ajaxError("颜色配置失败");
	}
	
}